<?php

namespace app\store\controller;

use app\store\model\Courses as CoursesModel;
use app\store\model\CoursesCategory as CoursesCategoryModel;
use app\store\service\Courses as CoursesService;

/**
 * 课程管理控制器
 * Class Courses
 * @package app\store\controller
 */
class Courses extends Controller
{
    /**
     * 课程列表(出售中)
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 获取全部课程列表
        $model = new CoursesModel;
        $list = $model->getList(array_merge(['status' => -1], $this->request->param()));
        // 课程分类
        $catgory = CoursesCategoryModel::getCacheTree();

        return $this->fetch('index', compact('list', 'catgory'));
    }

    /**
     * 添加课程
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch(
                'add',
                array_merge(CoursesService::getEditData(null, 'add'), [])
            );
        }
        $model = new CoursesModel;
        if ($model->add($this->postData('courses'))) {
            return $this->renderSuccess('添加成功', url('courses/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 一键复制
     * @param $Courses_id
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function copy($Courses_id)
    {
        // 课程详情
        $model = CoursesModel::detail($Courses_id);
        if (!$this->request->isAjax()) {
            return $this->fetch(
                'edit',
                array_merge(CoursesService::getEditData($model, 'copy'), compact('model'))
            );
        }
        $model = new CoursesModel;
        if ($model->add($this->postData('Courses'))) {
            return $this->renderSuccess('添加成功', url('Courses/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 课程编辑
     * @param $courses_id
     * @return array|bool|mixed
     */
    public function edit($courses_id)
    {
        // 课程详情
        $model = CoursesModel::detail($courses_id);
        if (!$this->request->isAjax()) {
            return $this->fetch(
                'edit',
                array_merge(CoursesService::getEditData($model), compact('model'))
            );
        }
        // 更新记录
        if ($model->edit($this->postData('courses'))) {
            return $this->renderSuccess('更新成功', url('courses/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改课程状态
     * @param $Courses_id
     * @param boolean $state
     * @return array
     */
    public function state($courses_id, $state)
    {
        // 课程详情
        $model = CoursesModel::detail($courses_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除课程
     * @param $Courses_id
     * @return array
     */
    public function delete($Courses_id)
    {
        // 课程详情
        $model = CoursesModel::detail($Courses_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}
